<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      title="客户转移"
      :visible.sync="dialogVisible"
      width="460px">
      <el-form :model="form" size="mini" label-width="100px" ref="form"  :rules="rules">
        <el-form-item label="类型" prop="type" v-if="type == 'one'">
          <el-radio-group v-model="form.type" @change="form.target = ''">
            <el-radio :label="1">客户负责人（包含所有产品线）</el-radio>
            <el-radio :label="2">产品线</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="转移到：" prop="target">
          <el-select v-model="form.target" filterable placeholder="请选择" clearable>
            <el-option
              v-if="form.type == 1"
              :key="0"
              label="公海"
              :value="0">
            </el-option>
            <el-option
              v-if="form.type == 1"
              :key="999999"
              label="资源池"
              :value="999999">
            </el-option>
            <el-option
              v-for="item in list"
              :key="item.userId"
              :label="item.realName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="转移产品线" prop="goodsLineIds" v-if="form.type == 2">
            <el-select multiple v-model="form.goodsLineIds">
                  <el-option v-for="item in goodsLineList" :label="item.name" :value="item.id" :key="item.id"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="备注：" prop="remark">
          <el-input v-model="form.remark" :autosize="{ minRows: 2, maxRows: 6}" type="textarea" placeholder="备注必填"></el-input>
        </el-form-item>
      </el-form>
      <div v-if="form.target === 0" style="font-size:12px;color:red">
        公海池： 客户使用塑料原料，未使用公司主营塑料原料
      </div>
      <div v-if="form.target === 999999" style="font-size:12px;color:red;line-height: 20px">
        资源池： 客户用到我们的主营品种PA,PC,ABS.PBT.BPA<br/>
        ①业务员在跟进进阶上因为个人原因有很大的困难<br/>
        ②贸易客户，不想多花时间跟进<br/>
        ③微小客户，不想花时间跟进<br/>
      </div>
      <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="handleConfirm" :loading="btnLoading">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {getStaffsList} from '@/api/management'
import { moveCustomer} from '@/api/company.js'
const defaultForm = {
  dataIds:[],
  type:1,
  target:'',
  goodsLineIds:[],
  remark:'',
}
export default {
  name: "moveCutomer",

  data() {

    return {
      dialogVisible: false,
      form: JSON.parse(JSON.stringify(defaultForm)),
      btnLoading:false,
      multipleSelection:[],
      rules:{
        type:[{required: true, message: '此项必填', trigger: 'change'},],
        target:[{required: true, message: '此项必填', trigger: 'change'},],
        goodsLineIds:[{required: true, message: '此项必填', trigger: 'change'},],
        remark:[{required: true, message: '此项必填', trigger: 'blur'},],
      },
      list:[],
      goodsLineList:[],
      type:''
    };
  },
  created() {

  },
  methods: {
    getUserList(){
      getStaffsList({pageNum:1,pageSize:1000}).then(response => {
        this.list = response.list
      });
    },

    showPop(multipleSelection,type) {
      this.type = type
      this.form = JSON.parse(JSON.stringify(defaultForm))
      this.goodsLineList = [];
      if(type == 'one'){
        let name = multipleSelection[0].goodsLineNames.split(",")
        let id = multipleSelection[0].goodsLineIds.split(",")
        id.forEach((el,index)=>{
          this.goodsLineList.push({name:name[index],id:el})
        })
      }else{
        this.form.type = 1;
      }
      this.dialogVisible = true
      this.multipleSelection = multipleSelection
      this.getUserList()
    },

    // 关闭弹框
    handleClose() {
      this.dialogVisible = false;
      this.btnLoading = false

    },
    //确认转移
    handleConfirm(){
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          let data =  JSON.parse(JSON.stringify(this.form));
          let dataIds=[];
          for(let i=0;i<this.multipleSelection.length;i++){
            dataIds.push(this.multipleSelection[i].dataId);
          }
          data.dataIds = dataIds;
          console.log("data",data)
          moveCustomer(data).then((res)=>{
            this.btnLoading = false;
            this.handleClose();
            this.$emit('getList')
          }).catch(()=>{
            this.btnLoading = false;
          })
        }
      })

    },

  },
};
</script>
<style lang="scss">
.avatar-uploader-icon {
  margin-bottom: 10px;
  display: inline-block;
  width: 80px;
  height: 80px;
  border: 1px solid #dcdfe6;
  text-align: center;
  line-height: 80px;
  vertical-align: middle;
}
</style>
